#!/usr/bin/env python
# -*- coding: UTF8 -*-

# preferences.py
#
#  Copyright (c) 2006 Marcelo Lira dos Santos
#
#  Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import os
from qemu_manager import QemuManager

class Preferences:

    def __init__(self, filename='portemu.cfg', qemu_manager=None):

        user_dir = os.path.expanduser('~')
        portemu_dir = os.path.join(user_dir, '.portemu')
        config_file = os.path.join(portemu_dir, filename)

        if not os.path.exists(portemu_dir):
            os.mkdir(portemu_dir)
            self.create_new_config_file(config_file)

        elif not os.path.exists(config_file):
            self.create_new_config_file(config_file)

        self.config_file = config_file
        self.qemu_manager = qemu_manager

        if self.qemu_manager:
            self.load()

    def load(self):
        f = open(self.config_file, 'r')
        qemu_bin, bios_dir, img_file, \
        serial_port, parallel_port = f.read().split('\n')[:-1]
        f.close()
        print 'load'
        print (qemu_bin, bios_dir, img_file, serial_port, parallel_port)
        self.__update_qemu(qemu_bin, bios_dir, img_file,
                           int(serial_port), int(parallel_port))

    def save(self, qemu_bin, bios_dir, img_file, serial_port, parallel_port):
        self.__update_qemu(qemu_bin, bios_dir, img_file,
                           serial_port, parallel_port)

        print 'save'
        print (qemu_bin, bios_dir, img_file, serial_port, parallel_port)

        f = open(self.config_file, 'w')
        f.writelines([qemu_bin + '\n', bios_dir + '\n', img_file + '\n',
                      str(serial_port) + '\n', str(parallel_port) + '\n'])
        f.close()

    def __update_qemu(self, qemu_bin, bios_dir, img_file,
                      serial_port, parallel_port):
        print self.qemu_manager
        self.qemu_manager.set_property('qemu-binary', qemu_bin)
        self.qemu_manager.set_property('bios-dir', bios_dir)
        self.qemu_manager.set_property('image-file', img_file)
        self.qemu_manager.set_property('serial-port', serial_port)
        self.qemu_manager.set_property('parallel-port', parallel_port)
        print self.qemu_manager

    def create_new_config_file(self, filepath):
        defaults = ['/usr/bin/qemu\n', '/usr/share/qemu\n',
                    '\n', '4444\n', '4445\n']
        f = open(filepath, 'w')
        f.writelines(defaults)
        f.close()

